/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.display.FilterBuilder;
import fever.display.ModalScreen;
import fever.display.text.AntiAliasType;
import fever.display.text.TextFieldAutoSize;
import fever.display.text.TextFormatAlign;
import fever.utils.Stringifier;

import flash.filters.BitmapFilter;

/**
 * Simple splash screen implementation.
 * 
 * <p>As splash screen should be not customizable, image used must be 
 * embded in your swf. ( no external customization is allowed ).
 * 
 * <p>Example
 * {@code
 *   var splash : fever.display.SplashScreen = fever.display.SplashScreen.getInstance();
 *   splash.setBackgroundColor( 0xffffff );
 *   splash.setBackgroundAlpha( 100 );
 *   splash.setImage( 'myImage' ); //image id defined in your swf ( embded )
 *   splash.addFilter( FilterBuilder.getSoftShadowFilter() );
 *   splash.show();
 * }
 * 
 * <p>Feel free to extend it to define more complex splash screen.
 * 
 * @author Romain Ecarnot
 */
class fever.display.SplashScreen
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : SplashScreen;
	
	private var _modal : ModalScreen;
	private var _image : MovieClip;
	private var _blField : TextField;	private var _trField : TextField;	private var _isOpened : Boolean;	private var _imageID : String;
	private var _hideDelay : Number;
	private var _interval : Number;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------	
	
	/**
	 *Auto hide delay in seconds. ( default splash screen is not auto hide ).
	 */
	public function get autoHideDelay() : Number { return _hideDelay; }
	public function set autoHideDelay ( p : Number ) : Void{ _hideDelay = p;	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns unique {@link fever.display.SplashScreen} instance.
	 * 
	 * <p>Useful to use always the same screen throw application life.
	 */
	public static function getInstance() : SplashScreen
	{
		if( !_instance ) _instance = new SplashScreen();
		return _instance;	
	}
	
	/**
	 * Constructor.
	 */
	public function SplashScreen()
	{
		_modal = new ModalScreen( 0x000000, 0 );
		_hideDelay = 0;
		
		hide();
		
		_configureUI();
	}
	
	/**
	 * Sets image id to display in screen.
	 */
	public function setImage( imageID : String ) : Void
	{
		if( imageID != _imageID )
		{
			_imageID = imageID;
			
			if( _isOpened ) _attachBitmap();
		}
	}
	
	/**
	 * Adds passed-in {@code filter} to image.
	 */
	public function addFilter( filter : BitmapFilter ) : Void
	{
		FilterBuilder.addFilter( _modal.getModalTarget(), filter );	
	}
	
	/**
	 * Cleans all filters applied on image.
	 */
	public function cleanFilter() : Void
	{
		FilterBuilder.clean( _modal.getModalTarget() );	
	}
	
	/**
	 * Sets background color.
	 */
	public function setBackgroundColor( color : Number ) : Void
	{
		_modal.setColor( color );
	}
	
	/**
	 * Returns background color.
	 */
	public function getBackgroundColor() : Number
	{
		return _modal.getColor();	
	}
	
	/**
	 * Sets background alpha value.
	 */
	public function setBackgroundAlpha( alpha : Number ) : Void
	{
		_modal.setAlpha( alpha );
	}
	
	/**
	 * Returns background alpha value.
	 */
	public function getBackgroundAlpha() : Number
	{
		return _modal.getAlpha();	
	}
	
	/**
	 * Sets splash text.
	 */
	public function setText( t : String ) : Void
	{
		_blField.text = t;
	}
	
	/**
	 * Returns splash text.
	 */
	public function getText() : String
	{
		return _blField.text;	
	}
	
	/**
	 * Sets application version text.
	 */
	public function setVersion( t : String ) : Void
	{
		_trField.text = t;
	}
	
	/**
	 * Returns application version text.
	 */
	public function getVersion() : String
	{
		return _trField.text;	
	}
	
	/**
	 * Displays screen.
	 */
	public function show() : Void
	{
		if( !_isOpened )
		{
			_isOpened = true;
			
			_attachBitmap();
			
			onResize();
			Stage.addListener( this );
			
			_modal.show();
			_modal.getModalTarget()._visible = true;
			
			if( _hideDelay > 0 ) 
				_interval = setInterval( this, 'hide', _hideDelay );
		}	
	}
	
	/**
	 * Hides screen.
	 */
	public function hide() : Void
	{
		Stage.removeListener( this );
		
		_modal.hide();
		_modal.getModalTarget()._visible = false;
			
		_isOpened = false;	
	}
	
	/**
	 * Disposes screen.
	 */
	public function dispose() : Void
	{
		hide();
		
		_image.removeMovieClip();
		_blField.removeTextField();		_trField.removeTextField();
		_modal.dispose();
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _configureUI() : Void
	{
		_image = _modal.getModalTarget().createEmptyMovieClip( '_img_', 1 );
		
		var tf : TextFormat = new TextFormat( '_sans', 10, 0x333333 );
		tf.align = TextFormatAlign.LEFT;
		
		_blField = _createTextField( '_bl_', 2, tf, TextFieldAutoSize.LEFT );
		
		tf.align = TextFormatAlign.RIGHT;
		_trField = _createTextField( 'tr', 3, tf, TextFieldAutoSize.RIGHT );
	}
	
	private function _createTextField( name : String, depth : Number, format : TextFormat, auto : String ) : TextField
	{
		var t : TextField = _modal.getModalTarget().createTextField( name, depth, 0, 2, 100, 20 );
		t.antiAliasType = AntiAliasType.ADVANCED;
		t.autoSize = auto;
		t.selectable = false;
		t.setNewTextFormat( format );
		
		return t;	
	}
	
	private function _attachBitmap() : Void
	{
		_image.attachMovie( _imageID, '_i_', 1 );	
	}
	
	private function onResize() : Void
	{
		var target : MovieClip = _modal.getModalTarget();
		
		target._x = ( Stage.width - target._width ) / 2;		target._y = ( Stage.height - target._height ) / 2;
		
		_blField._x = 5;
		_blField._y = _image._height - 18; //hard coded text height
		
		_trField._x = _image._width - _trField._width - 5;
		_trField._y = 0;
	}
}